package org.iron.services;

import java.io.IOException;

import org.apache.tapestry5.json.JSONObject;

import com.jcabi.http.request.JdkRequest;

public class WeatherServiceImpl implements WeatherService
{

    @Override
    public JSONObject getWeather(String location)
    {
        JSONObject json = null;
        try
        {
            String data = new JdkRequest("http://api.map.baidu.com/telematics/v3/weather").uri().queryParam("location", location).queryParam("output", "json")
                    .queryParam("ak", "zhqD7tiSIGkL0QRxBY5xwIss").back().fetch().body();
            json = new JSONObject(data);

        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return json;
    }

    @Override
    public String getPictureUrl(String weather)
    {
        String pictureUrl = "";

        switch (weather)
        {
            case "小雨":
                pictureUrl = "a_7.gif";
                break;
            case "阴":
                pictureUrl = "a_2.gif";
                break;
            case "多云":
                pictureUrl = "a_1.gif";
                break;
            case "小雨转阴":
                pictureUrl = "a_7.gif";
                break;
            case "晴":
                pictureUrl = "a_0.gif";
                break;
            case "阵雨":
                pictureUrl = "a_21.gif";
                break;
            case "雷阵雨":
                pictureUrl = "a_4.gif";
                break;
            case "雷阵雨伴有冰雹":
                pictureUrl = "a_5.gif";
                break;
            case "雨夹雪":
                pictureUrl = "a_6.gif";
                break;
            case "中雨":
                pictureUrl = "a_9.gif";
                break;
            case "大雨":
                pictureUrl = "a_3.gif";
                break;
            case "暴雨":
                pictureUrl = "a_12.gif";
                break;
            case "大暴雨":
                pictureUrl = "a_25.gif";
                break;
            case "特大暴雨":
                pictureUrl = "a_12.gif";
                break;
            case "阵雪":
                pictureUrl = "a_14.gif";
                break;
            case "小雪":
                pictureUrl = "a_15.gif";
                break;
            case "中雪":
                pictureUrl = "a_16.gif";
                break;
            case "大雪":
            case "暴雪":
                pictureUrl = "a_17.gif";
                break;
            case "雾":
                pictureUrl = "a_18.gif";
                break;
            case "冻雨":
                pictureUrl = "a_19.gif";
                break;
            case "沙尘暴":
                pictureUrl = "a_20.gif";
                break;
            case "强沙尘暴":
            case "霾":
                pictureUrl = "a_30.gif";
                break;
            case "小雨转中雨":
                pictureUrl = "b_23.gif";
                break;
            case "中雨转大雨":
            case "大雨转暴雨":
            case "暴雨转大暴雨":
            case "大暴雨转特大暴雨":
                pictureUrl = "b_25.gif";
                break;
            case "小雪转中雪":
            case "中雪转大雪":
            case "大雪转暴雪":
                pictureUrl = "b_28.gif";
                break;
            case "浮尘":
                pictureUrl = "a_29.gif";
                break;
            case "扬沙":
                pictureUrl = "a_30.gif";
                break;
            case "阴转多云":
                pictureUrl = "b_2.gif";
                break;
            case "多云转阴":
                pictureUrl = "b_1.gif";
                break;
            default:
                pictureUrl = "a_0.gif";
                break;
        }

        return pictureUrl;
    }
}
